package com.bestsonic.crawler.model;

import java.lang.reflect.InvocationTargetException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.Converter;

public class Bean<T> implements Regular<T> {
	private Split[] split;
	private Map<String, Regex> every;
	private Class<T> clazz;
	static{
		Converter converter = new Converter() {
			private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
			@SuppressWarnings("unchecked")
			@Override
			public <T> T convert(Class<T> type, Object value) {
				Date date = null;
				try {
					date = (Date) type.newInstance();
					date = sdf.parse(value.toString());
				} catch (ParseException e) {
					e.printStackTrace();
				} catch (InstantiationException e) {
					e.printStackTrace();
				} catch (IllegalAccessException e) {
					e.printStackTrace();
				}
				return (T) date;
			}
		};
		ConvertUtils.register(converter, Date.class);
	}

	public Bean(Class<T> clazz, Split[] split, Map<String, Regex> every) {
		this.clazz = clazz;
		this.split = split;
		this.every = every;
	}

	public Split[] getSplit() {
		return split;
	}

	public void setSplit(Split[] split) {
		this.split = split;
	}

	public Map<String, Regex> getEvery() {
		return every;
	}

	public void setEvery(Map<String, Regex> every) {
		this.every = every;
	}

	public List<T> matches(String content, int flags) {
		List<T> list = new ArrayList<T>();
		List<String> contentList = new ArrayList<String>();
		contentList.add(content);

		try {
			if (split != null) {
				String[] splitStr = null;
				for (Split s : split) {
					for (String everyContent : contentList) {
						splitStr = everyContent.split(s.getSplit());
						if (s.getEnd() > splitStr.length || s.getEnd() == 0) {
							s.setEnd(splitStr.length);
						}
						splitStr = Arrays.copyOfRange(splitStr, s.getStart(),s.getEnd());
						contentList = Arrays.asList(splitStr);
					}
				}
				for (String str : splitStr) {
					// System.out.println(str);
					T t = (T) clazz.newInstance();
					for (Map.Entry<String, Regex> entry : every.entrySet()) {
						if(entry.getValue().matches(str, flags).size() > 0)
							BeanUtils.setProperty(t, entry.getKey(), entry.getValue().matches(str, flags).get(0).trim());
						else
							BeanUtils.setProperty(t, entry.getKey(), "空");
					}
					System.out.println(t);
					list.add(t);
				}
			} 
			else {
				T t = (T) clazz.newInstance();
				for (Map.Entry<String, Regex> entry : every.entrySet()) {
					if(entry.getValue().matches(content, flags).size() > 0){
						BeanUtils.setProperty(t, entry.getKey(), entry.getValue().matches(content, flags).get(0).trim());
					}
					else
						BeanUtils.setProperty(t, entry.getKey(), "空");
				}
				list.add(t);
			}
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		} catch (InstantiationException e) {
			e.printStackTrace();
		}

		return list;
	}

}
